<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传测试表单</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            box-shadow: 0 0 10px #eee;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        input[type="file"] {
            padding: 5px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .response {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            border: 1px solid #d6e9c6;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            border: 1px solid #ebccd1;
            color: #a94442;
        }
    </style>
</head>
<body>
    <h1>文件上传测试</h1>
    <form id="uploadForm" enctype="multipart/form-data">
        <!-- 额外表单字段 -->
        <div class="form-group">
            <label for="title">文件标题</label>
            <input type="text" id="title" name="title" placeholder="请输入文件标题" value="测试文件">
        </div>

        <div class="form-group">
            <label for="tags">标签（用逗号分隔）</label>
            <input type="text" id="tags" name="tags" placeholder="请输入标签" value="test,upload,html">
        </div>
        <div class="form-group">
            <label for="file_name">文件名称</label>
            <input type="text" id="file_name" name="file_name" placeholder="文件名称" value="video.mp4">
        </div>
        <div class="form-group">
            <label for="description">文件描述</label>
            <textarea id="description" name="description" rows="3" placeholder="请输入文件描述">这是通过 HTML 表单上传的测试文件</textarea>
        </div>

        <!-- 文件上传字段 -->
        <div class="form-group">
            <label for="file">选择文件</label>
            <input type="file" id="file" name="file" required>
        </div>

        <!-- 上传类型（与前面的 handler 对应） -->
        <input type="hidden" name="upload_type" value="stream">

        <!-- 空间名称（与火山引擎 VOD 空间对应） -->
        <div class="form-group">
            <label for="space_name">空间名称</label>
            <input type="text" id="space_name" name="space_name" placeholder="请输入空间名称" required>
        </div>
        
        <button type="submit">上传文件</button>
    </form>

    <!-- 响应显示区域 -->
    <div id="response" class="response"></div>

    <script>
        // 处理表单提交
        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault(); // 阻止表单默认提交行为

            const formData = new FormData(this);
            const responseDiv = document.getElementById('response');

            // 显示加载状态
            responseDiv.textContent = "正在上传...";
            responseDiv.className = "response";
            responseDiv.style.display = "block";

            // 发送上传请求
            fetch('http://localhost:8080/api/v1/upload/media/stream', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                // 解析响应为 JSON
                return response.json().then(data => ({
                    status: response.status,
                    data: data
                }));
            })
            .then(result => {
                // 显示响应结果
                responseDiv.textContent = JSON.stringify(result.data, null, 2);
                
                // 根据状态设置样式
                if (result.status === 200) {
                    responseDiv.className = "response success";
                } else {
                    responseDiv.className = "response error";
                }
            })
            .catch(error => {
                // 处理错误
                responseDiv.textContent = "上传失败: " + error.message;
                responseDiv.className = "response error";
            });
        });
    </script>
</body>
</html>